/*
 * To change this template, choose Tools | Templates
 * and open the template in the editor.
 */

package manualTest;

import java.io.IOException;
import java.io.InputStreamReader;
import java.io.PrintStream;
import java.net.InetSocketAddress;
import java.net.Socket;
import java.net.SocketException;
import java.util.logging.Level;
import java.util.logging.Logger;

/**
 *
 * @author steff
 */
public class HttpClient {
    
    public static void runClient(String host, int port) {
        int tries = 0;
        try {
            tries++;
            Socket serverSocket = new Socket();
            serverSocket.connect(new InetSocketAddress(host,port), 3000);
            PrintStream psOut = new PrintStream(serverSocket.getOutputStream());
            psOut.print(
                    "GET / HTTP/1.1\r\n" +
                    "aheader: akey\r\n\r\n");
            psOut.flush();
            InputStreamReader in = new InputStreamReader(serverSocket.getInputStream());
            //System.out.println("---------------------");
            int c;
            StringBuffer sb = new StringBuffer();
            while((c = in.read()) != -1) {
                
                sb.append((char)c);
            }
            if (!sb.toString().equals(
                                    "HTTP/1.0 200 OK\r\n"
                                    +"Content-Length: 44\r\n"
                                    +"Date: to, 14 feb 2008 20:49:30 CET\r\n"
                                    +"Content-Type: text/html\r\n"
                                    +"Server: DOOM WebServer Alpha\r\n\r\n"
                                    +"Dette er index.html\r\n\r\n"
                                    +"<img src=\"orly.png\"/>"))
                System.out.println("no");
            serverSocket.close();

        } catch (SocketException se) {
            //try again
            if (tries<100) {
                try {
                    Thread.sleep(20);
                    runClient(host, port);
                } catch (InterruptedException ex) {
                    System.out.println("Hest");
                }
            } else {
                System.err.println("Connection refused: "+se.toString());
            } 
        } catch (IOException ex) {
            //ex.printStackTrace();
            if (tries<100) {
                try {
                    Thread.sleep(20);
                    runClient(host, port);
                } catch (InterruptedException e) {
                    System.out.println("Hest");
                }
            } else {
                System.err.println("Could not connect to "+host+":"+port);
            } 
            
            
        }
    
    }
    


    public static void main(String[] args) {
        for (int i = 0; i<10; i++){
            Thread t = new Thread(new Runnable() {
                public void run() {
                    HttpClient.runClient("localhost", 8080);
                }
            });
            t.start();
            
        }
    }

}
